cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        this._speed = 5;
        this.left = cc.find("left",this.node.parent);
        this.right = cc.find("right",this.node.parent);
        this.top = cc.find("top", this.node.parent);
        this.bottom = cc.find("bottom", this.node.parent);
        this.hero = this.node;
        if(this.hero){
            this.local = this.hero.getPosition();
           // cc.log(this.hero.getPostion());
        }
        

        
       
        
        this.node.parent.on("joystick_move", this.handle, this.node);
        this.left.on(cc.Node.EventType.TOUCH_END, this.onBtClicked, this);
        this.right.on(cc.Node.EventType.TOUCH_END, this.onBtClicked, this);
        this.top.on(cc.Node.EventType.TOUCH_END, this.onBtClicked, this);
        this.bottom.on(cc.Node.EventType.TOUCH_END, this.onBtClicked, this);
    },
    onBtClicked: function(event){
        switch(event.target.name){
            case "left":
                this.moveLeft();
                break;
            case "right":
                this.moveRight();
                break;
            case "top":
                this.moveTop();
                break;
            case "bottom":
                this.moveBottom();
                break;
        }
    },
    moveLeft:function(){
        var i=1;
        var action = cc.moveTo(10,cc.p(1000,0));
        //var repest = cc.repeatForever(action);
        
        cc.find("background",this.node.parent).runAction(action);
    },
    moveRight:function(){
        var i=2000;
        var action = cc.moveTo(10,cc.p(-1000,0));
        //var repest = cc.repeatForever(action);
        cc.find("background",this.node.parent).runAction(action);
    },
    moveTop: function(){
        var action = cc.moveTo(10,cc.p(-500,-200));
        //var repest = cc.repeatForever(action);
        cc.find("background",this.node.parent).runAction(action);
    },
    moveBottom: function(){
        var action = cc.moveTo(10,cc.p(-500,200));
        //var repest = cc.repeatForever(action);
        cc.find("background",this.node.parent).runAction(action);
    },
    handle: function(event){
        var angle = event.getUserData().angle;
        if(angle){
            cc.log(angle);
        }
       
       // var action = cc.moveTo(this._speed,this.local);
       // this.hero.runAction(action);
        
    }
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
